package com.wwm.springcloud.authservice.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.xml.transform.Result;
import java.util.UUID;

/**
 * 全局异常处理
 * @author wwm
 */
@ControllerAdvice
public class GlobalExceptionHandler
{
    private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result MethodExceptionHandler(Exception exception) throws Exception
    {
        String errMsg = exception.getMessage();
        String requestId = UUID.randomUUID().toString();
        if (errMsg != null && errMsg.indexOf("&") > 0)
        {
            requestId = errMsg.split("&")[0];
            errMsg = errMsg.split("&")[1];
        }
        logger.error("{}调用错误，原因：{}",requestId,errMsg);

        exception.printStackTrace();

        return null;
    }
}